package com.sandking.db.jedis;

import com.sandking.db.bean.Yhfk;
import com.sandking.tools.SK_Plus;
import com.sandking.tools.SK_Jedis;
import java.util.List;
import com.sandking.db.dao.YhfkDao;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Jedis;

public class YhfkJedis{
	/**
	 * 根据( id ) 查询
	 */
	public static Yhfk getById(int id){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		Yhfk yhfk = null;
		try{
			String key = "Yhfk_Object";
			String field = SK_Plus.b("id:",id).e();
			yhfk = Yhfk.createForJson(jedis.hget(key,field));
			if(yhfk == null){
				yhfk = YhfkDao.getById(id);
				if(yhfk!=null){
					loadCache(yhfk);
				}
			}
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
		
		return yhfk;
	}
	

	public static void loadCache(Yhfk yhfk){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			loadCache(yhfk,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 加载一个缓存
	 */
	public static void loadCache(Yhfk yhfk,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhfk_Object";
		String field = SK_Plus.b("id:",yhfk.getId()).e();
		String data = yhfk.toJson();
		p.hset(key,field,data);
		
	}
	
	/**
	 * 加载一组缓存
	 */
	public static void loadCaches(List<Yhfk> yhfks){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhfk yhfk : yhfks){
				loadCache(yhfk,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static void clearCache(Yhfk yhfk){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			clearCache(yhfk,p);
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	/**
	 * 清空一个缓存
	 */
	public static void clearCache(Yhfk yhfk,Pipeline p){
		// ---------------------- 主键索引 ----------------------
		String key = "Yhfk_Object";
		String field = SK_Plus.b("id:",yhfk.getId()).e();
		p.hdel(key,field);
		
	}
	/**
	 * 清空一组缓存
	 */
	public static void clearCaches(List<Yhfk> yhfks){
		Jedis jedis = SK_Jedis.getInstance().getJedis();
		try{
			Pipeline p = jedis.pipelined();
			for(Yhfk yhfk : yhfks){
				clearCache(yhfk,p);
			}
			p.sync();
		}catch (Exception e) {
			
		}finally{
			 SK_Jedis.getInstance().returnJedis(jedis);
		}
	}
	
	public static Yhfk insert(Yhfk yhfk){
		yhfk = YhfkDao.insert(yhfk);
		if(yhfk!=null){
			loadCache(yhfk);
		}
    	return yhfk;
    }
    
    public static Yhfk update(Yhfk yhfk){
    	yhfk = YhfkDao.update(yhfk);
    	if(yhfk!=null){
			loadCache(yhfk);
		}
    	return yhfk;
    }
    
    public static boolean delete(Yhfk yhfk){
    	boolean bool = YhfkDao.delete(yhfk);
    	if(bool){
    		clearCache(yhfk);
    	}
    	return bool;
    }
}